package book.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class ACharacterFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        //对请求对象做增强：添加字符集以支持中文
        //获取请求方法
        String method = req.getMethod();
        if("post".equalsIgnoreCase(method)){
            req.setCharacterEncoding("utf-8");
        }
        //调用下一个过滤器
        chain.doFilter(req, res);
    }
}
